//
//  LSZCacheHandling.m
//  JKFuYinProject
//
//  Created by 九酷 on 2017/10/31.
//  Copyright © 2017年 LC. All rights reserved.
//

#import "LSZCacheHandling.h"

@implementation LSZCacheHandling
#pragma mark - 查看里面有没有数据
+(void)initWithfindPlist:(NSString *)strPlistName :(plistData)block
{
    // 1.获得沙盒根路径
//    NSFileManager * fileManager = [NSFileManager defaultManager];
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path1 = [pathArray objectAtIndex:0];
    NSString * strName = [NSString stringWithFormat:@"%@.plist",strPlistName];
    NSString *myPath = [path1 stringByAppendingPathComponent:strName];
    NSDictionary * diction = [[NSDictionary alloc]initWithContentsOfFile:myPath];
 
    BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:myPath];
    if (blHave == YES)
    {
        //存在这个文件夹
        if (diction.allKeys.count >0) {
             block(YES,diction);//缓存在有效期之内
//            NSString * timeStr = [NSString stringWithFormat:@"%@",diction[@"plistTime"]];
//          NSString * stringTime  =  [LSZSpecialPackaging GetThePreviousTimeAndCompareItNow:timeStr];
//            if([stringTime rangeOfString:@"天"].location != NSNotFound ||[stringTime rangeOfString:@"月"].location != NSNotFound||[stringTime rangeOfString:@"年"].location != NSNotFound)
//            {
//                //有对应的文件夹
//                [LSZSpecialPackaging Reachability:^(NSString * strMessage) {
//                    if ([strMessage isEqualToString:NoNetwork]) {
//                        //没有网络的情况下
//                        block(YES,diction);//删除失败
//                    }else{
//                    //进行删除
//                        BOOL blDele= [fileManager removeItemAtPath:myPath error:nil];
//                        if (blDele) {
//                            block(NO,nil);//删除成功
//                        }else {
//                            block(YES,diction);//删除失败
//                        }
//                    }
//                }];
//            }else{
//                 block(YES,diction);//缓存在有效期之内
//            }
        }else{
            block(NO,diction);//没有数据
        }
    }else{
        block(NO,diction);//不存在这个文件夹
     }
}
#pragma mark - 删除对应文件夹的数据
+(void)initWithdeletingPlist:(NSString *)strPlistName :(plistData)block
{
    NSFileManager* fileManager=[NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    //文件名
    NSString * strName = [NSString stringWithFormat:@"%@.plist",strPlistName];
    NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:strName];
    BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];
    if (!blHave) {
        //不存在对应的文件夹
        block(YES,nil);
    }else {
        //有对应的文件夹
        BOOL blDele= [fileManager removeItemAtPath:uniquePath error:nil];
        if (blDele) {
            block(YES,nil);//删除成功
         }else {
             block(NO,nil);//删除失败
        }
    }
}
#pragma mark - 把请求到的数据添加进去 添加不为空判断
+(void)initAddPathPraisePoetry :(NSString *)strPlistName NSDictionary:(NSDictionary *)dic
{
    NSMutableDictionary * mutDic = [[NSMutableDictionary alloc]initWithDictionary:dic];
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [path objectAtIndex:0];
    NSString * strName = [NSString stringWithFormat:@"%@.plist",strPlistName];
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:strName];
    //获取当前的时间
    NSString * GetTheCurrentMonthDay = [LSZSpecialPackaging GetTheCurrentMonthDay];
    [mutDic setValue:GetTheCurrentMonthDay forKey:@"plistTime"];
    NSMutableArray *  arrayDat = [[NSMutableArray alloc]initWithArray:mutDic[@"data"]];
     for ( int A = 0; A <arrayDat.count; A ++) {
         NSDictionary *diction = mutDic[@"data"][A];
         NSMutableDictionary * mutDicParticulars = [[NSMutableDictionary alloc]initWithDictionary:diction];
        NSArray *arrayKey = mutDicParticulars.allKeys;
        NSArray * arrayValue = mutDicParticulars.allValues;
        for (int i = 0; i<arrayKey.count; i++) {
            NSString * strValue = [NSString stringWithFormat:@"%@",arrayValue[i]];
            if ([strValue  rangeOfString:@"null"].location !=NSNotFound) {
                NSString * strKey = [NSString stringWithFormat:@"%@",arrayKey[i]];
                strValue=[strValue stringByReplacingOccurrencesOfString:@"<null>"withString:@""];
                [mutDicParticulars setObject:@"" forKey:strKey];
                NSMutableDictionary * dicXiuGai = [[NSMutableDictionary alloc]initWithDictionary:mutDicParticulars];
                [arrayDat removeObjectAtIndex:A];
                [arrayDat  insertObject:dicXiuGai atIndex:A];
            }
        }
    }
    [mutDic setObject:arrayDat forKey:@"data"];
    BOOL didWriteSuccessfull = [mutDic writeToFile:plistPath atomically:YES];
    if (didWriteSuccessfull == YES) {
        NSLog(@"缓存成功");
    }else{
        NSLog(@"缓存失败");
    }
}


@end
